//
//  OrderViewController.m
//  Wy_BuyBooks
//
//  Created by wangyang on 2020/2/6.
//  Copyright © 2020 wangyang. All rights reserved.
//

#import "OrderViewController.h"
#import "BuyVc.h"
#import "DeliveryVc.h"
#import "CompletedVc.h"
@interface OrderViewController ()<SGSegmentedControlDelegate,UIScrollViewDelegate>

@property (nonatomic,strong)   SGSegmentedControl   *WySegmentedControl;
@property (nonatomic,strong)   UIScrollView         *WyScrollView;
@property (nonatomic,strong)   NSArray              *WyArray;


@end

@implementation OrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(initSetUI) name:@"initSetUI" object:nil];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(iniSetChild) name:@"iniSetChild" object:nil];
    [self initSetUI];
    [self iniSetChild];

      
}

-(void)initSetUI
{
   
    _WyArray = @[@"已购买",@"配送中",@"已完成"];
    _WyScrollView = [[UIScrollView alloc]init];
    [self.view addSubview:_WyScrollView];
    _WyScrollView.frame = CGRectMake(0, 0, self.view.frame.size.width, WySCREENHEIGHT);
    _WyScrollView.contentSize = CGSizeMake(self.view.frame.size.width*_WyArray.count, 0);
    _WyScrollView.pagingEnabled = YES;
    _WyScrollView.bounces = NO;
    _WyScrollView.showsVerticalScrollIndicator = NO;
    _WyScrollView.delegate = self;
    _WyScrollView.backgroundColor = [UIColor clearColor];
    
    
    BuyVc  *Delivered = [[BuyVc alloc]init];
    [self.WyScrollView addSubview:Delivered.view];
//    Delivered.view.frame = CGRectMake(0, 44, self.view.frame.size.width, WySCREENHEIGHT - NavViewHeight - 30 - 44);
    Delivered.view.frame = CGRectMake(0, 44, WySCREENWIDTH, WySCREENHEIGHT - NavViewHeight - 44  - SafeDistanceHeight);
//    NSLog(@"%f",NavViewHeight);

    [self addChildViewController:Delivered];
    
    
    
    
    _WySegmentedControl = [SGSegmentedControl segmentedControlWithFrame:CGRectMake(BiLiWidth(0), BiLiWidth(0), self.view.frame.size.width, 44) delegate:self segmentedControlType:SGSegmentedControlTypeStatic titleArr:_WyArray];
    [self.view addSubview:_WySegmentedControl];
    _WySegmentedControl.titleColorStateSelected = UIColorFromRGB(0xFADB14);
    _WySegmentedControl.indicatorColor = UIColorFromRGB(0xFADB14);
    _WySegmentedControl.backgroundColor = [UIColor whiteColor];
//    WyViewBorder(_WySegmentedControl, UIColorFromRGB(0x1896FF), 1);
    
   
   
    
    
    
}

- (void)SGSegmentedControl:(SGSegmentedControl *)segmentedControl didSelectBtnAtIndex:(NSInteger)index {
    CGFloat X = index * self.view.frame.size.width;
    
    self.WyScrollView.contentOffset = CGPointMake(X, 0);
    
    
    [self showVc:index];
}
-(void)iniSetChild
{
    
     DeliveryVc  *Delivery = [[DeliveryVc alloc]init];
      [self addChildViewController:Delivery];
      
      CompletedVc  *Completed = [[CompletedVc alloc]init];
      [self addChildViewController:Completed];
  
    
}
- (void)showVc:(NSInteger)index {
    
    CGFloat X = index * self.view.frame.size.width;
    
    UIViewController *vc = self.childViewControllers[index];
    
    if (vc.isViewLoaded)
        return;
    
    [self.WyScrollView addSubview:vc.view];
    vc.view.frame = CGRectMake(X, 44, self.view.frame.size.width, WySCREENHEIGHT - NavViewHeight - 30 );
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    NSInteger index = scrollView.contentOffset.x / scrollView.frame.size.width;
    
    [self showVc:index];
    
    [self.WySegmentedControl titleBtnSelectedWithScrollView:scrollView];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
